package acn.spring.backend;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import acn.spring.domain.Item;

public class ItemDaoHibernate extends HibernateDaoSupport implements ItemDao {

	@Override
	public List<Item> getAllItems() {
		@SuppressWarnings("unchecked")
		List<Item> items = getHibernateTemplate().find("from Item");
		return items;
	}

	@Override
	public Item addItem(Item item) {
		getHibernateTemplate().save(item);
		return item;
	}

	@Override
	public Item getItemWithId(int itemId) {
		@SuppressWarnings("unchecked")
		List<Item> itemList = getHibernateTemplate().find("from Item i where i.id = ?", itemId);
		
		if(itemList != null) {
			return itemList.get(0);
		}
		else 
			return null;
	}

	@Override
	public void deleteItem(int id) {
		Item item = getItemWithId(id);
		getHibernateTemplate().delete(item);
	}

}
